﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.mycenterusers
{
    public partial class order : System.Web.UI.Page
    {
        Model.sf_user_info userInfo;
        string appWhere;

        //标签 订单信息列表、是否支付
        public string orderMsg;
        int pageSize = 5;
        BLL.sf_goods_order orderInfoBll = new BLL.sf_goods_order();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*验证用户是否登录*/
                if (Session[DTKeys.SESSION_ADMIN_INFO] == null)
                {
                    Response.Redirect("index.aspx");
                }
                userInfo = Session[DTKeys.SESSION_ADMIN_INFO] as Model.sf_user_info;
                appWhere = Utils.getWhereByInfo(userInfo.busnieseNum, userInfo.appNum, userInfo.templatesNum);
            }
            int pageIndex = string.IsNullOrEmpty(hiddenPageIndex.Value) ? 1 : Convert.ToInt32(hiddenPageIndex.Value);
            int dataNum = string.IsNullOrEmpty(hiddenDataNum.Value) ? 0 : Convert.ToInt32(hiddenDataNum.Value);
            BindDataMsg(pageIndex, dataNum);
        }

        //上一页
        protected void btnPgUp_Click(object sender, EventArgs e)
        {
            int pageIndex = Convert.ToInt32(hiddenPageIndex.Value);
            int dataNum = Convert.ToInt32(hiddenDataNum.Value);
            pageIndex = pageIndex <= 1 ? pageIndex : pageIndex - 1;
            BindDataMsg(pageIndex, dataNum);
        }

        //下一页
        protected void btnPgDn_Click(object sender, EventArgs e)
        {
            int pageIndex = Convert.ToInt32(hiddenPageIndex.Value);
            int dataNum = Convert.ToInt32(hiddenDataNum.Value);
            pageIndex = pageIndex * pageSize < dataNum ? pageIndex + 1 : pageIndex;
            BindDataMsg(pageIndex, dataNum);
        }

        /// <summary>
        /// 绑定页面数据信息
        /// </summary>
        public void BindDataMsg(int pageIndex, int dataNum)
        {
            orderMsg = "";
            //根据条件 获取订单列表
            DataSet orderList = orderInfoBll.GetList(pageSize, pageIndex, "userNum='" + userInfo.userNum + "' and "+appWhere, "isPay,id desc", out dataNum, "");
            hiddenDataNum.Value = dataNum.ToString();
            hiddenPageIndex.Value = pageIndex.ToString();
            if (pageIndex <= 1)
            {
                btnPgUp.Enabled = false;
                btnPgUp.Style.Add("background-color", "#626670");
                btnPgUp.Style.Add("border-color", "#626670");
            }
            else
            {
                btnPgUp.Enabled = true;
                btnPgUp.Style.Add("border-color", "rgb(240, 35, 135)");
                btnPgUp.Style.Add("background-color", "rgb(240, 35, 135)");
            }
            if (pageIndex * pageSize >= dataNum)
            {
                btnPgDn.Enabled = false;
                btnPgDn.Style.Add("background-color", "#626670");
                btnPgDn.Style.Add("border-color", "#626670");
            }
            else
            {
                btnPgDn.Enabled = true;
                btnPgDn.Style.Add("border-color", "rgb(240, 35, 135)");
                btnPgDn.Style.Add("background-color", "rgb(240, 35, 135)");
            }

            for (int i = 0; i < orderList.Tables[0].Rows.Count; i++)
            {
                orderMsg += "<div class='space10'></div><div class='cartlistinner clearfix'><div class='cart_orderlist_info'><a href='orderdetail.aspx?orderId=" + orderList.Tables[0].Rows[i]["orderNum"] + "&type=" + orderList.Tables[0].Rows[i]["isPay"] + "'><p class='cart_g_name'>订 单 号：<span class='c-grey'>" + orderList.Tables[0].Rows[i]["orderNum"] + "</span></p><p class='cart_b_name'>支付方式：<span class='c-grey'>" + payWay(Convert.ToInt32(orderList.Tables[0].Rows[i]["payWay"])) + "</span></p><p class='cart_b_name'>创建时间：<span class='c-grey'>" + orderList.Tables[0].Rows[i]["addTime"] + "</span></p><p class='space5'></p></a></div><a class='cart_orderlist_p' href='' target='_top' style='display: block;'><span class='c_price'>¥" + orderList.Tables[0].Rows[i]["totalMoney"] + "</span></a>";
                if (Convert.ToInt32(orderList.Tables[0].Rows[i]["isPay"]) > 1)
                {
                    orderMsg += "<p class='end'>已付款</p></div>";
                }
                else
                {
                    orderMsg += "<div><a class='btn btn-large btn-purple end' href='javascript:;' data-islogin='true' data-shopcarthide='true' data-countdown='true' data-carttime='1197' onclick=\"orderBuy('" + orderList.Tables[0].Rows[i]["orderNum"] + "')\">结算</a><a class='ord_del end' href='javascript:;' onclick=\"del('" + orderList.Tables[0].Rows[i]["orderNum"] + "');\">取消订单</a></div></div>";
                }
            }
            orderMsg = string.IsNullOrEmpty(orderMsg) ? "<div class='space10'></div><div class='cartlistinner clearfix'><div class='cart_orderlist_info'><p class='cart_g_name'><span class='c-grey'>亲，您还没有订单！ 快去下单吧</span></p></div></div>" : orderMsg;
        }

        public string payWay(int? way)
        {
            string strStatus = "";
            switch (way)
            {
                case 0:
                    strStatus = "他人支付";
                    break;
                case 1:
                    strStatus = "微支付";
                    break;
            }
            return strStatus;
        }

    }
}